package com.teamgame.teamgame;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.List;

public class MyActivity extends ActionBarActivity {

    ListView listView;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        listView = (ListView) findViewById(R.id.listView);

        Common common = new Common(this);
        final List<Dictionary> dictionaries = common.findDictionary();

        String[] values = new String[dictionaries.size()];
        for (int i = 0; i < dictionaries.size(); i++) {
            values[i] = dictionaries.get(i).getId() + ". " + dictionaries.get(i).getWord();
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);

        listView.setAdapter(adapter);

        intent = new Intent(this, MainActivity.class);

        // ListView Item Click Listener
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Dictionary dictionary = dictionaries.get(position);
                intent.putExtra("id", dictionary.getId());
                startActivity(intent);
            }

        });

        common.closeDbHelper();
    }
}
